<?xml version="1.0"?>

<st-source>
<time-stamp>From VisualWorks® NonCommercial, 7.6 of March 3, 2008 on April 14, 2009 at 3:37:48 pm</time-stamp>


<class>
<name>Inventory</name>
<environment>SupplyChain</environment>
<super>Core.Object</super>
<private>false</private>
<indexed-type>none</indexed-type>
<inst-vars>items </inst-vars>
<class-inst-vars></class-inst-vars>
<imports></imports>
<category></category>
<attributes>
<package>SupplyChain</package>
</attributes>
</class>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<methods>
<class-id>SupplyChain.Inventory class</class-id> <category>instance creation</category>

<body package="SupplyChain" selector="new">new
	"Answer a newly created and initialized instance."

	^super new initialize</body>
</methods>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<comment>
<class-id>SupplyChain.Inventory</class-id>
<body>Thiss class represents an inventory of items

Instance Variables:
	items	&lt;SortedCollection&gt;	A sorted collection of items in the inventory

</body>
</comment>

<methods>
<class-id>SupplyChain.Inventory</class-id> <category>initialize-release</category>

<body package="SupplyChain" selector="initialize">initialize
	"Initialize a newly created instance. This method must answer the receiver."

	" *** Edit the following to properly initialize instance variables ***"
	items := SortedCollection new.
	items sortBlock:[:a :b | a name &lt; b name].
	^self</body>
</methods>

<methods>
<class-id>SupplyChain.Inventory</class-id> <category>accessing</category>

<body package="SupplyChain" selector="getItems">getItems
	^items</body>

<body package="SupplyChain" selector="hasItem:">hasItem: anItem
	"Tests if an item is in this inventory"

	^items contains: [ :i | i sku = anItem sku ]</body>

<body package="SupplyChain" selector="items">items
	^items</body>

<body package="SupplyChain" selector="items:">items: anObject
	items := anObject</body>

<body package="SupplyChain" selector="restockItem:">restockItem: anItem
	"Restocks an item"
	
	(self hasItem: anItem)
		ifFalse: [ self addItem: (Item named: (anItem name) sku: (anItem sku)) ].
	(self items detect: [ :i | i sku = anItem sku]) restockItem: anItem.</body>
</methods>

<methods>
<class-id>SupplyChain.Inventory</class-id> <category>required</category>

<body package="SupplyChain" selector="addItem:">addItem: anItem
	"Adds an Item to the items collection"
	(self items) add: anItem.
	^self.</body>

<body package="SupplyChain" selector="getInventoryCost">getInventoryCost
	"Returns the sum of all the item values"

	| sum |
	sum := 0.
	(self items) do:[:i | sum := sum + (i buyPrice).].
	^sum.</body>

<body package="SupplyChain" selector="getInventoryReport">getInventoryReport
	"Print a report"
	|profitSum output|
	profitSum := 0.
	output := (self printString).
	(self items) do:[:i |
		output := output, '\' withCRs, (i printString).
		profitSum := profitSum + (i potentialProfit)].
	output := output,'\' withCRs,('Total Inventory Potential Profit is ',(profitSum printString),' dollars').
	^output.</body>

<body package="SupplyChain" selector="getInventoryValue">getInventoryValue
	"Returns the sum of all the item values"

	| sum |
	sum := 0.
	(self items) do:[:i | sum := sum + (i itemValue).].
	^sum.</body>

<body package="SupplyChain" selector="getReorderItems">getReorderItems
	"Return a collection of all the items in the inventory that need to be reordered"

	| reorderItems |
	reorderItems := OrderedCollection new.
	(self items) do:[:i | (i needsReorder) ifTrue:[
			reorderItems add: i.].].
	^reorderItems.</body>

<body package="SupplyChain" selector="inventoryValue">inventoryValue
	"Returns the sum of all the item values"

	| sum |
	sum := 0.
	(self items) do:[:i | sum := sum + (i itemValue).].
	^sum.</body>

<body package="SupplyChain" selector="potentialProfit">potentialProfit
	"Return the sum of all the individual item potentialProfits"

	| profits |
	profits := 0.0.
	(self items) do:[:i | profits := profits + (i potentialProfit).].
	^profits.</body>

<body package="SupplyChain" selector="printInventoryReport">printInventoryReport
	"Print a report"
	|profitSum|
	profitSum := 0.
	Transcript show: (self printString).
	(self items) do:[:i |
		Transcript cr.
		Transcript show: (i printString).
		profitSum := profitSum + (i potentialProfit)].
	Transcript cr.
	Transcript show: ('Total Inventory Potential Profit is ',(profitSum printString),' dollars').
	^self.</body>

<body package="SupplyChain" selector="printString">printString
	"Returns a string representation of the Inventory as: 'An Inventory of n items valued at x dollars' "

	|itemSum totalValue|
	itemSum := ((self items) size).
	totalValue := 0.
	(self items) do:[:i |
		totalValue := totalValue + ((i buyPrice) * (i quantity)).].
	^('An Inventory of ',(itemSum printString),' items valued at ',(totalValue printString),' dollars').</body>

<body package="SupplyChain" selector="removeItem:">removeItem: anItem
	"Removes an Item from the items collection"
	((self items) isEmpty) ifFalse:[
		((self items) includes: anItem) ifTrue:[
			(self items) remove: anItem.].].
	^self</body>

<body package="SupplyChain" selector="sortBlock:">sortBlock: aBlock
	"Change the sort order of the items"
	
	(self items) sortBlock: aBlock.
	(((self items) size) &gt; 0) ifTrue:[ (self items) reSort.].
	^self.</body>
</methods>

</st-source>
